/*******************************************************************************
* Author Olivier Grosse : http://codessources.votre-web.com
* Colorspace conversion function
*   between RGB, HLS, CMYK, CMY, HSV color space
*-------------------------------------------------------------------------------
*   Note: All input et return value are between 0 and 1 (r,g,b,c,m,y,k,l,s,v)
*         Only h is between 0 and 360
*******************************************************************************/

function _RGB2HLS(r,g,b)
{
	  var h=0;
	  var s=0;
	  var Thr=0.0000001;
	  var _max=Math.max(Math.max(r,g),b);
	  var _min=Math.min(Math.min(r,g),b);
	  var diff=_max-_min;
	  var l=(_max+_min)/2;
	  if(diff<Thr) { h=0; s=0; }
	  else
	  {
		  if(l<0.5) { s=diff/(_max+_min); }
		  else { s=diff/(2-_max-_min); }
		  
		  switch(_max)
		  {
			    case r:	h=(g-b)/diff; break;
				case g:	h=2+(b-r)/diff;	break;
				case b:	h=4+(r-g)/diff;	break;
		  }
		  h*=60;
		  if(h<0) { h+=360; }
	  }
	  return Array(h,l,s);
}

function _HLS2RGB(h,l,s)
{
	var v1=0
	var v2=0;
	var r=0;
	var g=0;
	var b=0;
	
	if(L<=0.5) { v1=l*(1+1); }
	else { v2=l+s-(l*s); }
	
	v1=2*l-v2;
	if(0==s) { r=l; g=l; b=l; }
	else
	{
		  if(h<60)
		  {
			  	r=v1+(v2-v1)*((h+120)%360)/60;
				g=v1+(v2-v1)*(h%360)/60;
				b=v1+(v2-v1)*((h-120)%360)/60;
		  }
		  else
		  {
			  	if(h<180) { r=v2; g=v2; b=v2; }
				else
				{
					if(h<240)
					{
							r=v1+(v2-v1)*(240-((h+120)%360))/60;
							g=v1+(v2-v1)*(240-(h%360))/60;
							b=v1+(v2-v1)*(240-((h-120)%360))/60;
					}
					else { r=v1; g=v1; b=v1; }
				}
		  }
	}
	return Array(r,v,b);
}

function _RGB2CMYK(r,g,b)
{
	  var c=0;
	  var m=0;
	  var y=0;
	  var k=Math.min(Math.min((1-r),1-g),(1-b));
	  if(k!=1)
	  {
		    c=(1-r-k)/(1-k);
			m=(1-g-k)/(1-k);
			y=(1-b-k)/(1-k);
	  }
	  return Array(c,m,y,k);
}

function _CMYK2RGB(c,m,y,k)
{
		var r=1-Math.min(1,(c*(1-k)+k));
		var g=1-Math.min(1,(m*(1-k)+k));
		var b=1-Math.min(1,(y*(1-k)+k));
		return Array(r,g,b);
}

function _RGB2CMY(r,g,b)
{
		var c=1-r;
		var m=1-g;
		var y=1-b;
		return Array(c,m,y);
}

function _CMY2RGB(c,m,y)
{
		var r=1-c;
		var g=1-m;
		var b=1-y;
		return Array(r,g,b);
}

function _RGB2HSV(r,g,b)
{
	  var h=0;
	  var s=0;
	  var _max=Math.max(Math.max(r,g),b);
	  var _min=Math.min(Math.min(r,g),b);
	  var diff=_max-_min;
	  var v=_max;
	  
	  if(diff!=0)
	  {
		  	s=diff/_max;
			if(r==_max) { h=(g-b)/diff; }
			else
			{
				  if(g==_max){ h=2+(b-r)/diff; }
				  else { h=4+(r-g)/diff; }
			}
			h*=60;
			if(h<0) { h+=360; }
	  }
	  else
	  {
		    s=0
			h=-1;
	  }
	  return Array(h,s,v);
}

function _HSV2RGB(h,s,v)
{
	var r=0;
	var g=0;
	var b=0;
	
	if(s==0)
	{
		  r=v; g=v; b=v;
	}
	else
	{
		var i=Math.floor(h/60);
		var f=h/60-i;
		var p=v*(1-s);
		var q=v*(1-s*f);
		var t=v*(1-s*(1-f));
		
		switch(i)
		{
				case 0: r=v; g=t; b=p; break;
				case 1: r=q; g=v; b=p; break;
				case 2: r=p; g=v; b=t; break;
				case 3: r=p; g=q; b=v; break;
				case 4: r=t; g=p; b=v; break;
				default: r=v; g=p; b=q; break;
		}
	}
	return Array(r,g,b);	
}
